home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / client / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-23  |  5.7 KB  |  251 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "tweak.h"
  11. #include "client_def.h"
  12. #include "c_extern.h"
  13.  
  14. #ifndef NOLOCKING
  15. static char key_string[sizeof(KEY_PREFIX)+32];
  16.      
  17. static char code_str[] =
  18.        "0123456789:_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  19.      
  20. static void make_key_string PROTO2(unsigned long, server_addr,
  21.                    unsigned long, server_port)
  22. {
  23.   unsigned long v1, v2;
  24.   char *p;
  25.   
  26.   strcpy(key_string,KEY_PREFIX);
  27.   for(p = key_string; *p; p++);
  28.   v1 = server_addr;
  29.   v2 = server_port;
  30.   
  31.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  32.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  33.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  34.   v1 = v1 | (v2 << (32-3*6));
  35.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  36.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  37.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  38.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  39.   *p++ = code_str[v1 & 0x3f]; v1 >>= 6;
  40.   *p   = 0;
  41. }
  42. #endif
  43.  
  44. /********************************************************************/
  45. /******* For those systems that has flock function call *************/
  46. /********************************************************************/
  47. #ifdef USE_FLOCK
  48.  
  49. #include <sys/file.h>
  50.  
  51. int key_persists = 1;
  52. static unsigned int lock_fd;
  53. static unsigned short okey;
  54.  
  55. unsigned short client_get_key PROTO0((void))
  56. {
  57.   if(flock(lock_fd,LOCK_EX) == -1) {
  58.     perror("flock");
  59.     exit(1);
  60.   }
  61.   if(read(lock_fd,&okey,sizeof(okey)) == -1) {
  62.     perror("read"); exit(1);
  63.   }
  64.   if(lseek(lock_fd,0L,0) == -1) {
  65.     perror("seek");
  66.     exit(1);
  67.   }
  68.   return(okey);
  69. }
  70.  
  71. void client_put_key PROTO1(unsigned short, key)
  72. {
  73.   if(write(lock_fd,&key,sizeof(key)) == -1) {
  74.     perror("write");
  75.     exit(1);
  76.   }
  77.   if(lseek(lock_fd,0L,0) == -1) {
  78.     perror("seek");
  79.     exit(1);
  80.   }
  81.   if(flock(lock_fd,LOCK_UN) == -1) {
  82.     perror("unflock");
  83.     exit(1);
  84.   }
  85. }
  86.  
  87. void client_init_key PROTO3(unsigned long, server_addr,
  88.                 unsigned long, server_port,
  89.                 unsigned short, key)
  90. {
  91.   unsigned long omask;
  92.   okey = key;
  93.   
  94.   make_key_string(server_addr,server_port);
  95.   
  96.   omask = umask(0);
  97.   lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  98.   umask(omask);
  99. }
  100.  
  101. #endif
  102. /********************************************************************/
  103. /******* For those systems that has lockf function call *************/
  104. /********************************************************************/
  105. #ifdef USE_LOCKF
  106.  
  107. #ifdef HAVE_UNISTD_H
  108. #include <unistd.h>
  109. #endif
  110.  
  111. int key_persists = 1;
  112. static unsigned int lock_fd;
  113. static unsigned short okey;
  114.  
  115. unsigned short client_get_key PROTO0((void))
  116. {
  117.   if(lockf(lock_fd,F_LOCK,sizeof(okey)) == -1) {
  118.     perror("lockf");
  119.     exit(1);
  120.   }
  121.   if(read(lock_fd,&okey,sizeof(okey)) == -1) {
  122.     perror("read");
  123.     exit(1);
  124.   }
  125.   if(lseek(lock_fd,0L,0) == -1) {
  126.     perror("seek");
  127.     exit(1);
  128.   }
  129.   return(okey);
  130. }
  131.  
  132. void client_put_key PROTO1(unsigned short, key)
  133. {
  134.   if(write(lock_fd,&key,sizeof(key)) == -1) {
  135.     perror("write");
  136.     exit(1);
  137.   }
  138.   if(lseek(lock_fd,0L,0) == -1) {
  139.     perror("seek");
  140.     exit(1);
  141.   }
  142.   if(lockf(lock_fd,F_ULOCK,sizeof(key)) == -1) {
  143.     perror("unlockf");
  144.     exit(1);
  145.   }
  146. }
  147.  
  148. void client_init_key PROTO3(unsigned long, server_addr,
  149.                 unsigned long, server_port,
  150.                 unsigned short, key)
  151. {
  152.   unsigned long omask;
  153.   okey = key;
  154.   
  155.   make_key_string(server_addr,server_port);
  156.   
  157.   omask = umask(0);
  158.   lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  159.   umask(omask);
  160. }
  161.  
  162. #endif
  163. /********************************************************************/
  164. /******* For those systems that has SysV shared memory + lockf ******/
  165. /********************************************************************/
  166. #ifdef USE_SHAREMEM_AND_LOCKF
  167.  
  168. #ifdef HAVE_UNISTD_H
  169. #include <unistd.h>
  170. #endif
  171. #include <sys/ipc.h>
  172. extern char *shmat();
  173.  
  174. int key_persists = 0;
  175. static unsigned short *share_key;
  176. static unsigned int lock_fd;
  177.  
  178. unsigned short client_get_key PROTO0((void))
  179. {
  180.   if(lockf(lock_fd,F_LOCK,2) == -1) {
  181.     perror("lockf");
  182.     exit(1);
  183.   }
  184.   return(*share_key);
  185. }
  186.  
  187. void client_put_key PROTO1(unsigned short, key)
  188. {
  189.   *share_key = key;
  190.   if(lockf(lock_fd,F_ULOCK,2) == -1) {
  191.     perror("unlockf");
  192.     exit(1);
  193.   }
  194. }
  195.  
  196. void client_init_key PROTO3(unsigned long, server_addr,
  197.                 unsigned long, server_port,
  198.                 unsigned short, key)
  199. {
  200.   unsigned long omask;
  201.   key_t lock_key;
  202.   int   lock_shm;
  203.   
  204.   make_key_string(server_addr,server_port);
  205.   
  206.   omask = umask(0);
  207.   lock_fd = open(key_string,O_RDWR|O_CREAT,0666);
  208.   umask(omask);
  209.   
  210.   if((lock_key = ftok(key_string,3432)) == -1) {
  211.     perror("ftok");
  212.     exit(1);
  213.   }
  214.   if((lock_shm = shmget(lock_key,sizeof(short),IPC_CREAT|0666)) == -1) {
  215.     perror("shmget");
  216.     exit(1);
  217.   }
  218.   if(!(share_key = (unsigned short *) shmat(lock_shm,(char*)0,0))) {
  219.     perror("shmat");
  220.     exit(1);
  221.   }
  222. }
  223.  
  224. #endif
  225. /********************************************************************/
  226. /******* For those who does not want to use locking *****************/
  227. /********************************************************************/
  228. #ifdef NOLOCKING
  229.  
  230. int key_persists = 0;
  231. static unsigned short okey;
  232.  
  233. unsigned short client_get_key PROTO0((void))
  234. {
  235.   return(okey);
  236. }
  237.  
  238. void client_put_key PROTO1(unsigned short, key)
  239. {
  240.   okey = key;
  241. }
  242.  
  243. void client_init_key PROTO3(unsigned long, server_addr,
  244.                 unsigned long, server_port,
  245.                 unsigned short, key)
  246. {
  247.   okey = key;
  248. }
  249.  
  250. #endif
  251.